<?php

namespace App\Http\Controllers\Admin\Shop;

use App\Models\Shop\Order;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;

class OrderController extends Controller
{
   /**
    * Display a listing of the resource.
    *
    * @return \Illuminate\Http\Response
    */
   public function index(Request $request)
   {
      $where = function ($query) use ($request) {
         if ($request->has('keyword') and $request->keyword != '') {
            $cate = $request->cate;
            $search = "%" . $request->keyword . "%";
            if ($cate == 'name' || $cate == 'phone') {
               $query->where('b.' . $cate, 'like', $search);
            } else {
               $query->where('a.' . $cate, 'like', $search);
            }
         }

         //按时间搜索
         if ($request->has('date') && $request->date != '' && $request->date != null) {
            $time = $request->time;
            $query->whereBetween('a.' . $time, explode(',', $request->date));
         }

         //用户下的订单
         if ($request->has('user_id') && $request->user_id != '') {
            $query->where('a.user_id', '=', $request->user_id);
         }
      };

      $orders = DB::table('orders as a')
         ->leftJoin('users as b', 'a.user_id', '=', 'b.id')
         ->leftJoin('expresses as c', 'a.express_id', '=', 'c.id')
         ->select('a.*', 'b.name as user_name', 'b.phone', 'c.name as express_name')
         ->where($where)
         ->paginate(config('admin.page_size'));

      $payType = config('admin.pay_type');
      $orderStatus = config('admin.order_status');

      return response()->json(compact('orders', 'payType', 'orderStatus'));
   }

   /**
    * Display the specified resource.
    *
    * @param int $id
    * @return \Illuminate\Http\Response
    */
   public function show($id)
   {
      $order = DB::table('order_products as a')
         ->leftJoin('products as b', 'a.product_id', '=', 'b.id')
         ->leftJoin('categories as c', 'b.category_id', '=', 'c.id')
         ->select('b.*', 'a.num', 'c.name as category_name')
         ->where('a.order_id', $id)
         ->get();

      return response()->json($order);
   }

   /***
    *  编辑
    */
   public function edit($id)
   {
      $order = Order::find($id);
      return response()->json($order);
   }

   /**
    * Update the specified resource in storage.
    *
    * @param \Illuminate\Http\Request $request
    * @param int $id
    * @return \Illuminate\Http\Response
    */
   public function update(Request $request, $id)
   {
      $order = Order::find($id);
      $order->update([
         'express_id' => $request->express_id,
         'express_code' => $request->express_code,
         'express_money' => $request->express_money
      ]);
   }

   /**
    * Remove the specified resource from storage.
    *
    * @param int $id
    * @return \Illuminate\Http\Response
    */
   public function destroy($id)
   {
      Order::destroy($id);
      DB::table('order_products')->where('order_id', $id)->delete();  //删除中间表
   }

   /***
    *  收货地址
    */
   public function order_address(Request $request)
   {
      $order_addresses = DB::table('order_addresses')->where('order_id', '=', $request->id)->first();
      return response()->json($order_addresses);
   }

   /***
    *  更新收货地址
    */
   public function edit_address(Request $request)
   {
      DB::table('order_addresses')
         ->where('order_id', $request->order_id)
         ->update($request->all());
   }

}
